Copy List with Random Pointer
Question
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Analysis
- 将整个链表复制一遍,复制节点紧跟原节点
- 复制每个节点的random指针,复制节点的random指针同理指向复制节点
- 提取复制后的链表
- 复制过程需要两个指针,copy与copyiter,copy指向当前需要加入复制链表的节点,copyiter用来连接整个链表(移动的指针)
- 加入空表头,方便返回结果 copyiter=pseudo_head
Code
|
|
Clone Graph
Question
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
Analysis
利用BFS递归对图进行复制,map保存节点val与相应节点的映射关系
Code
|
|